package org.example.SolutionNew001;

import java.util.Arrays;

public class Array05 {
    public static void main(String[] args) {
        Array05 obj = new Array05();
        int[] nums=new int[]{7,8,9,11,12};
        System.out.println(obj.firstMissingPositive(nums));
    }

    public int firstMissingPositive1(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            while(nums[i]<n+1&&nums[i]>0&&nums[i]!=i+1&&nums[nums[i]-1]!=nums[i]){
                int tmp=nums[nums[i]-1];
                nums[nums[i]-1]=nums[i];
                nums[i]=tmp;
            }
        }
//        System.out.println(Arrays.toString(nums));
        for (int i = 0; i < n; i++) {
            if(nums[i]!=i+1){
                return i+1;
            }
        }
        return n+1;
    }

    public int firstMissingPositive(int[] nums) {
        int n=nums.length;
        for (int i = 0; i < n; i++) {
            if(nums[i]<=0){
                nums[i]=n+1;
            }
        }
        for (int i = 0; i < n; i++) {
            int num=Math.abs(nums[i]);
            if(num<n+1){
                nums[num-1]=-Math.abs(nums[num-1]);
            }
        }
        for (int i = 0; i < n; i++) {
            if(nums[i]>0){
                return i+1;
            }
        }
        return n+1;
    }
}
